﻿/** \file
* \$Rev: 187 $
* 
* \$Date: 2010-04-27 21:58:51 +0000 (Tue, 27 Apr 2010) $
*
* \$URL: http://chorus-twitter.googlecode.com/svn/trunk/src/net/View.cs $
*/

using System;
using System.Collections;
using System.Collections.Generic;

namespace chorus.net
{
	/// <summary>
	/// A view object, constituting a collection of sources that each issues
	/// some messages.
	/// </summary>
	[Serializable]
	public class View : gui.IDisplayRenamable, IDisposable
	{
		/// <summary>The associated view form.</summary>
		gui.ViewForm form;
		/// <summary>The display name.</summary>
		string name;
		/// <summary>The display name.</summary>
		public string DisplayName
		{
			get { return name; }
			set
			{
				name = value;
				form.Text = value;
			}
		}
		/// <summary>The list of source containers.</summary>
		public List<SourceContainer> Sources { get; set; }

		/// <summary>The constructor.</summary>
		public View()
		{
			Sources = new List<SourceContainer>();
			form = Program.mainform.AddView(this);
		}

		/// <summary>The destructor. Destroy the associated form.</summary>
		void IDisposable.Dispose() { form.CloseWithoutPrompt(); }

		/// <summary>Fetch messages from all contained sources asynchronously.</summary>
		public void FetchAsync()
		{
			foreach (SourceContainer source in Sources)
				source.Source.FetchAsync(Deserialize);
		}

		/// <summary>The asynchronous deserialization callback.</summary>
		/// <param name="response">The received response object.</param>
		void Deserialize(twitter.IMessageCollectionResponse response)
		{
			if (response != null)
			{
				foreach (twitter.Message message in response.Messages)
					form.AddMessage(message);
			}
		}
	}
}
